<?php

class Socios extends CI_Controller
{
	function __construct()
    {
        parent::__construct();
        $this->load->model('socios_model');
		$this->load->helper(array('form', 'url'));
		$this->load->helper('generic');
    }

	public function nuevo($arr = array())
	{
		redirectIfNotAdmin();
		$datos['CargadorScript'] = "public/js/socios/Nuevo.js";
		$this->load->model('carreras_model');
		$datos['title'] = "Biblioteca Juan Carlos";
		$datos['navBarDerecho'] = imprimirNavBarDerecho();
		$datos['jumbotron'] = "Cargar Socio";
		$carreras['ddlCarreras'] = $this->carreras_model->getAll();
		//by rube
		if($arr)
        $carreras['vector'] = $arr; 
		//
		$datos['content'] = $this->load->view('socios/socioNuevo', $carreras, TRUE);
		$this->load->view('master_view',$datos);
	}

	public function pedidoBase(){
		//sleep(0.5);
		$datos = $this->socios_model->getByDni($this->input->post('dni'));
        if($datos == null)
        	echo 'libre';
        else echo 'ocupado';
	}
	
	public function index()
    {
          $socio = $this->socios_model->getByDni('37369589');
		  echo $socio->nombre."<br>";
		  $this->load->helper('date');
		  echo now();
    }
	
	public function todos()
	{
		$socios = $this->socios_model->getAll();
		foreach ($socios as $socio)
		{
				echo $socio['apellido'];
		}
	}
	
	public function insert()
	{	 
    	//si se ha pulsado el botón submit validamos el formulario con codeIgniter
		//if($this->input->post('submit'))
		//{
			$arr = array('dni'=>$this->input->post('txtDNI'),
		      	'nombre'=>$this->input->post('txtNombre'),
		      	'apellido'=>$this->input->post('txtApellido'),
		      	'direccion'=>$this->input->post('txtDireccion'),
		      	'mail'=>$this->input->post('txtMail'),
		      	'telefono'=>$this->input->post('txtTelefono'),
		      	'socio'=>$this->input->post('ddlTipoUsuario'),
		      	'carrera'=>$this->input->post('ddlCarrera'),
		      	'legajo'=>$this->input->post('txtLegajo')
		      	);
				
			$vec = $this->socios_model->getByDni($this->input->post('txtDNI'));
			if($vec != null)
			{
				
		         $this->nuevo($arr);
			}
			else
			{
			$this->socios_model->dni = $this->input->post('txtDNI');
			$this->socios_model->password = substr($this->input->post('txtDNI'),-6);
			$this->socios_model->nombre = $this->input->post('txtNombre');
			$this->socios_model->apellido = $this->input->post('txtApellido');
			$this->socios_model->direccion = $this->input->post('txtDireccion');
			$this->socios_model->mail = $this->input->post('txtMail');		
			$this->socios_model->telefono = $this->input->post('txtTelefono');
			if($this->input->post('ddlTipoUsuario') == "Socio")
				$this->socios_model->admin = 0;
			else 
				$this->socios_model->admin = 1;

			$test = $this->socios_model->insert();
			if ($this->db->_error_message())
			{
				echo "asd";
			    mysql_error("error en la conexion");
			    $datos['content'] = $this->load->view('socios/socioNuevo', $carreras, TRUE);
				$this->load->view('master_view',$datos);
			}

			
			
			$this->load->model('carnets_model');
			$this->load->helper('date');
			$this->carnets_model->dni = $this->input->post('txtDNI');
			$this->carnets_model->idCarrera = $this->input->post('ddlCarrera');
			$this->carnets_model->legajo = $this->input->post('txtLegajo');
			$this->carnets_model->fecha = date("Y-m-d H:i:s");
			$this->carnets_model->insert();
			
			     //start of file upload code
             $config['upload_path'] = './public/dni/';
             $config['allowed_types'] = 'gif|jpg|png|jpeg';
			 $config['file_name'] = $this->input->post('txtDNI') . ".png"  ;
             $this->load->library('upload', $config);
            if ( ! $this->upload->do_upload())
			{
				$error = array('error' => $this->upload->display_errors());

				//$this->load->view('upload_form', $error);
			}
            $data = $this->upload->data();
            //end of file upload code
			
		$this->load->model('carreras_model');
		$arr['carrera'] = $this->carreras_model->getById($this->input->post('ddlCarrera'))->descripcion;
		$datos['title'] = "Carnet Nuevo | Biblioteca Juan Carlos";
		$datos['navBarDerecho'] = imprimirNavBarDerecho();
		$datos['jumbotron'] = "Carnet Nuevo";
		$datos['content'] = $this->load->view('carnets/carnet_nuevo', $arr, TRUE);
		$this->load->view('master_view',$datos);
			}
		//}
	}

    public function PrestamosActuales()
    {
        $datos['title'] = "Prestamos_actuales_socio";
        $datos['navBarDerecho'] = imprimirNavBarDerecho();
        $datos['jumbotron'] = "";
        $datos['content'] = $this->load->view('vistas_nuevas/Prestamos_actuales_socio','',true);
        $this->load->view('master_view',$datos);
    }

    public function HistorialPrestamos()
    {
  	$datos['title'] = "Historial_prestamo_socio";
    $datos['navBarDerecho'] = imprimirNavBarDerecho();
    $datos['jumbotron'] = "";
    $datos['content'] = $this->load->view('vistas_nuevas/Historial_prestamo_socio','',true);
    $this->load->view('master_view',$datos);
   }

    public function BuscarSocios()
    {
        $datos['title'] = "Biblioteca Juan Carlos";
        $datos['navBarDerecho'] = imprimirNavBarDerecho();
        $datos['jumbotron'] = "";
        $datos['content'] = $this->load->view('vistas_nuevas/Buscar_socio','',true);
        $this->load->view('master_view',$datos);
    }
	
	public function administrar()
	{
		
	}

}

?>